package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.mapper.EduTeacherMapper;
import com.example.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-22
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {


    //查询所有banner
    @Cacheable(value = "teacher", key = "'selectIndexteacher'")
    @Override
    public List<EduTeacher> selectTeacherList() {
        //前四名讲师
        QueryWrapper<EduTeacher> teacheryWrapper=new QueryWrapper<>();
        teacheryWrapper.orderByDesc("id");
        teacheryWrapper.last("limit 4");
        List<EduTeacher> eduTeachers = baseMapper.selectList(teacheryWrapper);
        return eduTeachers;
    }


    //查询前端页面讲师
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> teacherPage) {
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据封装到pageteacher对象
        baseMapper.selectPage(teacherPage,wrapper);

        List<EduTeacher> records = teacherPage.getRecords();
        long current = teacherPage.getCurrent();
        long pages = teacherPage.getPages();
        long size = teacherPage.getSize();
        long total = teacherPage.getTotal();
        boolean hasNext = teacherPage.hasNext();
        boolean hasPrevious = teacherPage.hasPrevious();


        //把分页查询放到map集合中
        Map<String,Object> map=new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        //返回map
        return map;


    }
}
